package com.ygxsk.carhome.ui.livefee;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.lxj.xpopup.core.BottomPopupView;
import com.lxj.xpopup.util.XPopupUtils;
import com.ygxsk.carhome.R;
import com.ygxsk.carhome.constant.ConstantSign;
import com.ygxsk.carhome.constant.ExtraValue;
import com.ygxsk.carhome.listener.RxBus;
import com.ygxsk.carhome.response.FeePayTypeListBean;
import com.ygxsk.carhome.rxbus.RxBusWithFeePayTypeListBean;
import com.ygxsk.carhome.ui.adapter.FeePayTypePopuAdatper;
import com.ygxsk.carhome.views.MyLinearLayoutManager;

import java.util.ArrayList;
import java.util.Iterator;

/*
 * @Author:itchenqi175@163.com
 * @Time:2023-12-21  10:52
 * @Description:支付的弹窗面板，不同的支付方式,准备做一个通用界面,这里的标题，都用这个花，那么
 */
public class FeePayPopu extends BottomPopupView {

    //因为通用界面，标志从哪个页面过来；
    private int extra_jumpfrom_where;
    private Context mContext;

    //支付方式适配器
    private FeePayTypePopuAdatper feePayTypePopuAdatper;

    //携带外部给予的支付列表；
    private ArrayList<FeePayTypeListBean> feePayTypeListDatas_outSide = new ArrayList<>();

    public FeePayPopu(@NonNull Context context) {
        super(context);
    }

    public FeePayPopu(@NonNull Context _context, int _extra_action, ArrayList<FeePayTypeListBean> _feePayTypeListDatas) {
        super(_context);

        extra_jumpfrom_where = _extra_action;

        this.mContext = _context;

        if (_feePayTypeListDatas != null && !_feePayTypeListDatas.isEmpty()) {

            if (feePayTypeListDatas_outSide != null && !feePayTypeListDatas_outSide.isEmpty()) {
                feePayTypeListDatas_outSide.clear();
            }

            if (feePayTypeListDatas_outSide != null) {
                feePayTypeListDatas_outSide.addAll(_feePayTypeListDatas);
            }

        }

//        LogUtils.d(ConstantSign.LOGCAT_NOMAL + "station_list_outSide--size==>" + card_type_list_outSide.size());
    }

    @Override
    protected int getImplLayoutId() {//当前的这个条目内容
        return R.layout.feepaypopu;
    }

    @Override
    protected void onCreate() {
        super.onCreate();

        //LogUtils.d(ConstantSign.LOGCAT_NOMAL + "onCreate--onCreate---onCreate--onCreate--onCreate");

        //取消按钮
        TextView btn_dialog_search_cancel = (TextView) this.findViewById(R.id.btn_dialog_search_cancelfee);
        //确定按钮
        TextView btn_dialog_search_decide = (TextView) this.findViewById(R.id.btn_dialog_search_decidefee);

        //搜索编辑框；
//        ClearEditText et_searchInCustomSmall = (ClearEditText) this.findViewById(R.id.et_searchincustomsmallpo);
        //搜索的文字按钮；
//        TextView textViewSearchInCustomSmall = (TextView) this.findViewById(R.id.textviewsearchincustomsmallpo);

        //初始化适配器列表
        RecyclerView recyclerViewInCardType = (RecyclerView) this.findViewById(R.id.recyclerviewinpaytype);
//        MyLinearLayoutManager myLinearLayoutManager = new MyLinearLayoutManager(this.getContext(), LinearLayoutManager.VERTICAL, false);
//        recyclerViewInCardType.setLayoutManager(myLinearLayoutManager);
        //bug 报错 2024年1月16日08:58:13 bad window token
        recyclerViewInCardType.setLayoutManager(new MyLinearLayoutManager(mContext));

        //适配器的创建；
        feePayTypePopuAdatper = new FeePayTypePopuAdatper(R.layout.feepaytypepopuadatper, feePayTypeListDatas_outSide);

        if (feePayTypePopuAdatper != null) {
            recyclerViewInCardType.setAdapter(feePayTypePopuAdatper);
            feePayTypePopuAdatper.notifyDataSetChanged();
        }

        //单独的点击条目
        feePayTypePopuAdatper.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {

                //2023-6-7 10:45:18 清空上次的选中状态，防止这次选中.
                //首先全部清空
                //直接设置勾选；
                FeePayTypeListBean feePayTypeListBeanPosition = (FeePayTypeListBean) adapter.getItem(position);

                if (feePayTypeListBeanPosition != null) {

                    //发送消息，给业务台账，填充选中的业务类型；
                    for (FeePayTypeListBean itemFeePayTypeList : feePayTypePopuAdatper.getData()) {

                        //是唯一的，
                        // TODO: 2023-07-25 19:47 将来的判断要保持唯一；才能保证点击条目正常
                        if (itemFeePayTypeList.getPayTypeId() == feePayTypeListBeanPosition.getPayTypeId()) {

                            if (itemFeePayTypeList.isChecked()) {
                                itemFeePayTypeList.setChecked(false);
                            } else {
                                itemFeePayTypeList.setChecked(true);
                            }

                            LogUtils.json("itchen--选中的", itemFeePayTypeList);

                        } else {
                            itemFeePayTypeList.setChecked(false);
                        }

                    }

                    //是单选，直接刷新；
                    if (feePayTypePopuAdatper != null) {
                        feePayTypePopuAdatper.notifyDataSetChanged();
                    }

                } else {
                    ToastUtils.showShort(StringUtils.getString(R.string.action_abnormal));
                }

            }
        });

        //【取消】按钮
        if (btn_dialog_search_cancel != null) {
            btn_dialog_search_cancel.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
        }

        //【确定】按钮
        if (btn_dialog_search_decide != null) {

            btn_dialog_search_decide.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {

                    //method_test();

                    //即将被传递的单个item;
                    FeePayTypeListBean feePayTypeListBean_upLoad = null;

                    if (feePayTypeListDatas_outSide != null && !feePayTypeListDatas_outSide.isEmpty()) {

                        //----------------------------------------------------------------------------------------

                        Iterator<FeePayTypeListBean> iterator = feePayTypeListDatas_outSide.iterator();

                        if (iterator != null) {

                            while (iterator.hasNext()) {

                                FeePayTypeListBean item = iterator.next();

                                if (item != null) {

                                    if (item.isChecked()) {

                                        LogUtils.json(ConstantSign.LOGCAT_NOMAL + "选中支付方式是->", item);

                                        feePayTypeListBean_upLoad = item;

                                        break;
                                    }

                                }
                            }
                        }

                        //----------------------------------------------------------------------------------------
                        if (feePayTypeListBean_upLoad != null) {

                            //标识是从哪个页面过来的；
                            //标志是 水费 过来的弹窗列表
//                            RxBusAssetManageStation rxBusAssetManageStation = new RxBusAssetManageStation();
//                            rxBusAssetManageStation.setActionFlag(ExtraValue.MODIFY_ASSET_MANAGE_STATION_SELECT);
//                            rxBusAssetManageStation.setAssetStationInfoBean(assetStationInfoBean_upLoad);
//                            RxBus.getInstance().post(rxBusAssetManageStation);

                            if (extra_jumpfrom_where == ExtraValue.MODIFY_EVALUATE_PAYTYPE_SELECT_FEEWATER_FRAGMENT) {

                                //如果是水费过来的
                                RxBusWithFeePayTypeListBean rxBusWithFeePayTypeListBean = new RxBusWithFeePayTypeListBean();
                                rxBusWithFeePayTypeListBean.setActionFlag(ExtraValue.MODIFY_EVALUATE_PAYTYPE_SELECT_FEEWATER_FRAGMENT);
                                rxBusWithFeePayTypeListBean.setFeePayTypeListBean(feePayTypeListBean_upLoad);
                                RxBus.getInstance().post(rxBusWithFeePayTypeListBean);

                            } else if (extra_jumpfrom_where == ExtraValue.MODIFY_EVALUATE_PAYTYPE_SELECT_ELECTRIC_FRAGMENT) {

                                //如果是电费过来的
                                RxBusWithFeePayTypeListBean rxBusWithFeePayTypeListBean = new RxBusWithFeePayTypeListBean();
                                rxBusWithFeePayTypeListBean.setActionFlag(ExtraValue.MODIFY_EVALUATE_PAYTYPE_SELECT_ELECTRIC_FRAGMENT);
                                rxBusWithFeePayTypeListBean.setFeePayTypeListBean(feePayTypeListBean_upLoad);
                                RxBus.getInstance().post(rxBusWithFeePayTypeListBean);

                            } else if (extra_jumpfrom_where == ExtraValue.MODIFY_EVALUATE_PAYTYPE_SELECT_PROPERTY_ACTIVITY) {

                                //如果是物业费过来的
                                RxBusWithFeePayTypeListBean rxBusWithFeePayTypeListBean = new RxBusWithFeePayTypeListBean();
                                rxBusWithFeePayTypeListBean.setActionFlag(ExtraValue.MODIFY_EVALUATE_PAYTYPE_SELECT_PROPERTY_ACTIVITY);
                                rxBusWithFeePayTypeListBean.setFeePayTypeListBean(feePayTypeListBean_upLoad);
                                RxBus.getInstance().post(rxBusWithFeePayTypeListBean);


                            } else if (extra_jumpfrom_where == ExtraValue.MODIFY_EVALUATE_PAYTYPE_SELECT_RENTAL_ACTIVITY) {

                                LogUtils.d(ConstantSign.LOGCAT_NOMAL + "如果是租赁费过来的");

                                //如果是租赁费过来的
                                RxBusWithFeePayTypeListBean rxBusWithFeePayTypeListBean = new RxBusWithFeePayTypeListBean();
                                rxBusWithFeePayTypeListBean.setActionFlag(ExtraValue.MODIFY_EVALUATE_PAYTYPE_SELECT_RENTAL_ACTIVITY);
                                rxBusWithFeePayTypeListBean.setFeePayTypeListBean(feePayTypeListBean_upLoad);
                                RxBus.getInstance().post(rxBusWithFeePayTypeListBean);

                            }

                            //关闭该弹窗内容
                            dismiss();

                        } else {
                            //请选择产品类型
                            ToastUtils.showShort(R.string.popu_select_one_item);
                        }

                    } else {
                        //执行异常？
                        ToastUtils.showShort(StringUtils.getString(R.string.action_abnormal));
                    }

                }
            });
        }


    }

    @Override
    public void onDestroy() {

//        LogUtils.d(ConstantSign.LOGCAT_NOMAL + "弹窗--弹窗--弹窗onDestroy--onDestroy---onDestroy--onDestroy--onDestroy");
        method_clear_item_state();
        super.onDestroy();

    }

    private void method_clear_item_state() {

        if (feePayTypeListDatas_outSide != null && !feePayTypeListDatas_outSide.isEmpty()) {

            for (FeePayTypeListBean feePayTypeListBean : feePayTypeListDatas_outSide) {
                feePayTypeListBean.setChecked(false);
            }

            if (feePayTypePopuAdatper != null) {
                feePayTypePopuAdatper.notifyDataSetChanged();
            }

        }
    }

    @Override
    protected boolean onBackPressed() {

//        LogUtils.d(ConstantSign.LOGCAT_NOMAL + "拦截 弹出弹窗的  返回事件");
//        CommUtils.checkDialog(alertViewDialogSure);
//        if (mDatasResources!=null&&!mDatasResources.isEmpty()){
//        }

        return super.onBackPressed();
    }

    @Override
    protected void onShow() {
        super.onShow();
    }

    @Override
    protected void onDismiss() {
        super.onDismiss();
    }

    @Override
    protected int getMaxHeight() {
//        return (int) (XPopupUtils.getScreenHeight(getContext()) * .6f);
        return (int) (XPopupUtils.getScreenHeight(getContext()) * .4f);
    }


}
